// pages/merchantDetial/merchantDetial.js
const app = getApp()
const request = require('../../utils/request')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    screenHeight: 0,
    tabBarIndex: 0,
    shopId: 0,
    productList: [],
    ticketList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('参数信息：', options)
    this.setData({
      screenHeight: wx.getSystemInfoSync().screenHeight,
      shopId: options.shopId
    })
    this.getPorductInShop()
    this.getCouponInShop()
  },
  /**
    * 切换tab
    */
  selectTab(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      tabBarIndex: index
    })
  },
  /**
  * swiper组件滑动切换事件
  */
  changeNearbyBar(e) {
    let index = e.detail.current
    this.setData({
      tabBarIndex: index
    })
  },
  /**
   * 获取商家商品
   */
  getPorductInShop() {
    let that = this
    request.http('/shop/shop-info/getShopGoodsListByShopInfo', 'GET', { shopInfoId: this.data.shopId })
      .then(res => {
        let data = res.data
        if (data.code == 200) {
          that.setData({
            productList: data.data
          })
        }
      })
  },
  /**
   * 购买商品页
   */
  productBuy(e) {
    let goods = e.currentTarget.dataset.item
    // let productList = this.data.productList
    wx.navigateTo({
      url: '/pages/productOrderRecord/productOrderRecord?id=' + goods.id
    })
  },
  /**
   * 获取商家优惠券
   */
  getCouponInShop() {
    let that = this
    request.http('/shop/shop-info/getShopTicketListByShopInfo', 'GET', { shopInfoId: this.data.shopId })
      .then(res => {
        let data = res.data
        if (data.code == 200) {
          that.setData({
            ticketList: data.data
          })
        }
      })
  },
  /**
   * 领券
   */
  getCoupon(e) {
    let index = e.currentTarget.dataset.index
    let ticketList = this.data.ticketList
    request.http('/customer/ticket/receive-ticket', 'POST', { ticketId: ticketList[index].id })
      .then(res => {
        let data = res.data
        if (data.code == 200) {
          wx.showToast({
            title: '领取成功',
          })
        }
      })
  }
})